import Axios from '@/util/http'
import Cookie from 'js-cookie'
import api from '@/config/api'
import Router from '@/router'
import { Message } from 'element-ui'

const state = {
  // 用户名
  name: '',
  // 用户登录PCToken
  PCToken: '',
  // 审核状态 1-待完善，2-待审核，3-待复核，4-已通过，5，未通过
  status: ''
}

const getters = {
  username: state => {
    return decodeURIComponent(state.name)
  }
}

const mutations = {
  setName: (state, data) => {
    if (data) {
      Cookie.set('username', data)
    } else {
      Cookie.remove('username')
    }
    state.name = data
  },
  setPCToken (state, data) {
    if (data) {
      Cookie.set('PCToken', data)
    } else {
      Cookie.remove('PCToken')
    }
    state.PCToken = data
  },
  setStatus: (state, data) => {
    if (data) {
      Cookie.set('confirmStatus', data)
    } else {
      Cookie.remove('confirmStatus')
    }
    state.status = data
  }
}

const actions = {
  // 账号密码登录
  loginByPSW ({dispatch, commit, state}, params) {
    return new Promise((resolve, reject) => {
      Axios.post(api + '/pc/customer/login', params).then(res => {
        if (res.responseCode === '0000') {
          commit('setPCToken', res.data.token)
          commit('setStatus', res.data.confirmStatus)
          if (res.data.customerName) {
            commit('setName', res.data.customerName)
          } else {
            commit('setName', res.data.defaultName)
          }
          Message({
            type: 'success',
            message: res.responseMsg,
            duration: 1500,
            onClose: () => {
              Router.push('/home')
            }
          })
        } else {
          Message.error({
            message: res.responseMsg
          })
        }
        return resolve()
      }).catch(err => {
        return reject(err)
      })
    })
  },
  // 短信验证码登录
  loginBySMS ({dispatch, commit, state}, params) {
    return new Promise((resolve, reject) => {
      Axios.post(api + '/pc/customer/login', params).then(res => {
        if (res.responseCode === '0000') {
          commit('setPCToken', res.data.token)
          commit('setStatus', res.data.confirmStatus)
          if (res.data.customerName) {
            commit('setName', res.data.customerName)
          } else {
            commit('setName', res.data.defaultName)
          }
          Message({
            type: 'success',
            message: res.responseMsg,
            duration: 1500,
            onClose: () => {
              Router.push('/home')
            }
          })
        } else {
          Message.error({
            message: res.responseMsg
          })
        }
        return resolve()
      }).catch(err => {
        return reject(err)
      })
    })
  },
  // 退出登录
  logout ({dispatch, commit, state}, params) {
    return new Promise((resolve, reject) => {
      Axios.post(api + '/pc/customer/logout', params).then(() => {
        commit('setPCToken', '')
        commit('setName', '')
        commit('setStatus', '')
        commit('shopcart/setShopcart', '', {root: true})
        Router.push('/login')
      })
    })
  },
  // 重新登录
  relogin ({dispatch, commit}) {
    return new Promise(resolve => {
      commit('setPCToken', Cookie.get('PCToken'))
      commit('setName', Cookie.get('username'))
      commit('setStatus', Cookie.get('confirmStatus'))
      return resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
